var _ = require('lodash');
var config = require('../../config');
var whost = config.whost.replace('api', 'boss'); // 业态服务走BOSS
var request = require('../../support').request2;

/**
 * 列举积分可以兑换的奖品
 * @return promise
 */
exports.listAward = function (context, params, page, size) {
  if (typeof params === 'string') {
    params = {
      goodsType: params
    };
  }
  return request(context, {
    url: whost + '/itbg/integralGoods/page',
    data: _.merge(params, {
      pageNo: page || 1,
      pageSize: size || 10,
    })
  }).then(function (data) {
    data.list = data.list || [];
    return data;
  });
};

/**
 * 列举优惠券
 * @return promise
 */
exports.listCoupons = function (contex, page, size) {
  return this.listAward(contex, 'TICKET', page, size);
};

/**
 * 列举商品
 * @return promise
 */
exports.listGoods = function (contex, page, size) {
  return this.listAward(contex, 'ENTITY', page, size);
};

/**
 * 列举流量包
 * @return promise
 */
exports.listPackage = function (contex, mobile, page, size) {
  return this.listAward(contex, {
    goodsType: 'FLOW',
    mobile: mobile,
  }, page, size).then(function (data) {
    data.list = data.list.sort(function (a, b) {
      return a.consumeIntegral > b.consumeIntegral;
    });
    return data;
  });
};

// 获取积分余额
exports.getIntegral = function (context, memberId) {
  return request(context, {
    url: whost + '/itbg/integralOrder/getAvailableIntegral',
    data: {
      mid: memberId,
    }
  }).then(function (data) {
    return {
      available: parseInt(data.entity.availableIntegral, 10)
    };
  }, function () {
    return null;
  });
};

/**
 * 商品详情
 * @return promise
 */
exports.details = function (contex, id, mid) {
  return request(contex, {
    url: whost + '/itbg/integralGoods/detail',
    data: {
      id: id,
      mid: mid,
    }
  });
};

/**
 * 下单
 * @return promise
 */
exports.placeorder = function (contex, data) {
  return request(contex, {
    url: whost + '/itbg/integralOrder/save',
    method: 'post',
    data: data,
  });
};

/**
 * 订单详情
 * @return promise
 */
exports.getOrder = function (contex, id) {
  return request(contex, {
    url: whost + '/itbg/integralOrder/detail',
    data: {
      id: id,
    }
  }).then(function (data) {
    if (data.entity.goodsType === 'FLOW') {
      data.entity.coverImg = '/assets/images/icons/pkg.png';
    }
    return data;
  });
};

/**
 * 列举订单
 * @return promise
 */
exports.listOrder = function (contex, mid, page, size) {
  return request(contex, {
    url: whost + '/itbg/integralOrder/page',
    data: {
      mid: mid,
      pageNo: page || 1,
      pageSize: size || 20,
    }
  }).then(function (data) {
    data.list.forEach(function (item) {
      if (item.goodsType === 'FLOW') {
        item.coverImg = '/assets/images/icons/pkg.png';
      }
    });
    return data;
  });
};

/**
 * 获取默认收货人
 * @param  {number} userId 用户ID
 * @return {promise}
 */
exports.defaultAddress = function (context, memberId) {
  return this.listAddress(context, memberId).then(function (res) {
    var address = null;
    var list = res.list || [];
    list.forEach(function (item) {
      if (Number(item.defaultFlag) === 1) {
        address = item;
      }
    });
    return address;
  }, function () {
    return null;
  });
};

/**
 * 获取指定的收货地址
 * @param  {number} addressId 地址ID
 * @return {promise}
 */
exports.getAddress = function (context, memberId, addressId) {
  return this.listAddress(context, memberId).then(function (data) {
    for (var i = 0; i < data.list.length; i++) {
      if (data.list[i].id === addressId) {
        return data.list[i];
      }
    }
    return null;
  }, function () {
    return null;
  });
};

/**
 * 列举收货人信息
 * @param  {number} memberId 用户ID
 * @param  {number} addressBusinessType      0//地址业态 业态类型:{0:通用,2:乐购,3:洗衣,4:洗车}
 * @return {promise}
 */
exports.listAddress = function (context, memberId) {
  return request(context, {
    url: config.whost + '/qding-api/api/json/user/selUserForReceiveMessage',
    method: 'post',
    data: {
      memberId: memberId,
      addressBusinessType: 0,
    }
  });
};

/**
 * 创建收货人地址
 * @return promise
 */
exports.addAddress = function (context, address) {
  return request(context, {
    url: config.whost + '/qding-api/api/json/user/addUserForReceiveMessage',
    method: 'post',
    data: address
  });
};

/**
 * 更新收货人地址
 * @return {promise}
 */
exports.setAddress = function (context, address) {
  return request(context, {
    url: config.whost + '/qding-api/api/json/user/updateUserForReceiveMessage',
    method: 'post',
    data: address
  });
};

/**
 * 更新或创建收货人地址
 * @return promise
 */
exports.saveOrUpdate = function (context, address) {
  return address.id ? this.setAddress(context, address) : this.addAddress(context, address);
};

//积分详情列表
exports.pointsDetail = function (contex, pars) {
  return request(contex, {
    url: config.whost + '/qding-api/api/json/points/pointsDetail',
    data: {
      memberId: pars.memberId,
      pageNo: pars.pageNo || 1,
      pageSize: pars.pageSize || 10
    }
  })
};

//积分收入明细
exports.pointsRevenueDetail = function (contex, pars) {
  return request(contex, {
    url: config.whost + '/qding-api/api/json/points/pointsRevenueDetail',
    data: {
      memberId: pars.memberId,
      pageNo: pars.pageNo || 1,
      pageSize: pars.pageSize || 10
    }
  });
};

//积分支出明细
exports.pointsExpendDetail = function (contex, pars) {
  return request(contex, {
    url: config.whost + '/qding-api/api/json/points/pointsExpendDetail',
    data: {
      memberId: pars.memberId,
      pageNo: pars.pageNo || 1,
      pageSize: pars.pageSize || 10
    }
  });
};